package com.kitty.game.reward.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.base.service.BagService;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.equip.service.FabaoService;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.reward.model.RewardSet;
import com.kitty.game.reward.model.RewardUnitSet;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.Const;
import com.kitty.common.model.Reason;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@Component
public class RewardService {
    /**检测能否给奖励(空间是否足够)*/
    public boolean checkAddReward(Role role, RewardSet rewardSet) {
        return checkAddReward(role, rewardSet.getBagPosCount(), rewardSet.getPetPosCount());
    }

    /**
    * 检测能否给奖励(空间是否足够)
     * count 够买总数
    */
    public boolean checkAddReward(Role role, RewardUnitSet rewardUnitSet, int count) {
        if(rewardUnitSet.getStoreType()==0){
            return true;
        }
        if(rewardUnitSet.getStoreType()==2){
            return checkAddReward(role, 1, rewardUnitSet.getPetPosCount());
        }
        return checkAddReward(role, rewardUnitSet.getPosCount() * count, rewardUnitSet.getPetPosCount() * count);
    }

    private boolean checkAddReward(Role role, int needBagCount, int needPetCount) {
        if (needBagCount > 0 && !SpringUtils.getBean(BagService.class).checkBagEnough(role, false, needBagCount)) {
            MessagePusher.notify2Player(role, I18nId.PMT_412);
            return false;
        }
        if (needPetCount > 0 && !SpringUtils.getPetService().checkPetPosEnouth(role, needPetCount)) {
            MessagePusher.notify2Player(role, I18nId.PMT_413);
            return false;
        }

        return true;
    }

    /**发奖励*/
    public void addReward(Role role, RewardSet rewardSet) {
        if (rewardSet == null) {return ;}
        for (RewardUnitSet rewardUnitSet : rewardSet.getList()) {
            addRewardItem(role, rewardUnitSet);
        }
    }

    private void addRewardItem(Role role, RewardUnitSet rewardUnitSet) {
        if (rewardUnitSet == null) {return ;}

        switch (rewardUnitSet.getType()) {
            /**道具*/


            case Const.REWARD_TYPE_ITEM:
                SpringUtils.getEquipService().addMall(rewardUnitSet.getName(), rewardUnitSet.getBind()==0, role, rewardUnitSet.getCount());
                break;
            /**首饰*/
            case Const.REWARD_TYPE_JEWELRY:
                if (rewardUnitSet.getName().contains(",")) {
                    String[] names = rewardUnitSet.getName().split(",");
                    int index = ThreadLocalRandom.current().nextInt(names.length);
                    String name = names[index];
                    /**暂时写死为相五属性的*/
                    SpringUtils.getEquipService().getJewelry(role, name, true, rewardUnitSet.getCount(),true);
                }else if (rewardUnitSet.getShowName().contains(("("))&&rewardUnitSet.getShowName().contains((")"))){
                    SpringUtils.getEquipService().getJewelryNew(role, rewardUnitSet.getName(), 1, rewardUnitSet.getShowName(),false);
                }else if (rewardUnitSet.getShowName().equals("50级首饰")){
                    SpringUtils.getEquipService().getRandomJewelry(50, role, false);
                }else if (rewardUnitSet.getShowName().equals("70级首饰")){
                    SpringUtils.getEquipService().getRandomJewelry(70, role, false);
                }else {
                    SpringUtils.getEquipService().getJewelry(role, rewardUnitSet.getName(), false, rewardUnitSet.getCount(),true);
                }
                break;
            /**代金券*/
            case Const.REWARD_TYPE_VOUCHER:
                SpringUtils.getRoleService().addVoucher(role, rewardUnitSet.getCount());
                break;
            /**守护*/
            case Const.REWARD_TYPE_GUARD:
                SpringUtils.getGuardService().addGuard(role, rewardUnitSet.getName());
                break;
            /**宠物*/
            case Const.REWARD_TYPE_PET:
                if (SpringUtils.getPetService().isEquippedFull(role,rewardUnitSet.getName())) {
                    MessagePusher.pushMessage(role, new RespMsg("对不起，你的宠物栏满了，请整理后再来吧。"));
                    return;
                }
                for (int i = 0; i < rewardUnitSet.getCount(); i++) {
                    SpringUtils.getPetService().addPet(role, rewardUnitSet.getName(), (short) 1, false,rewardUnitSet.getBind()==1);
                }
                break;
            /**法宝*/
            case Const.REWARD_TYPE_ARTIFACT:
                for (int i = 0; i < rewardUnitSet.getCount(); i++) {
                    if (rewardUnitSet.getLevel() == 18){
                        int[] ints = {1,2,3,4,5};
                        Random random = new Random();
                        int index = random.nextInt(ints.length);
                        SpringUtils.getBean(FabaoService.class).getArtifact18(role,rewardUnitSet.getName(), ints[index]);
                    }else {
                        SpringUtils.getEquipService().getArtifact(rewardUnitSet.getName(), role);
                    }
                }
                break;
            case Const.REWARD_TYPE_yinde:
                SpringUtils.getRoleService().addYinde(role,rewardUnitSet.getCount(),true);
                break;
            case Const.REWARD_TYPE_jinbi:
                SpringUtils.getRoleService().addMoney(role,rewardUnitSet.getCount());
                break;
            case Const.REWARD_TYPE_hunqi:
                for (int i = 0; i < rewardUnitSet.getCount(); i++) {
                    SpringUtils.getBean(EquipService.class).getEquip(role, rewardUnitSet.getName());
                }
                break;
            case Const.REWARD_TYPE_luoshuexp:
                SpringUtils.getRoleService().addluoshuExp(role,rewardUnitSet.getCount());
                break;
            case Const.REWARD_TYPE_wulingzhi:
                SpringUtils.getRoleService().addWuling(role,rewardUnitSet.getCount(),false);
                break;
            case Const.REWARD_TYPE_guiqi:
                SpringUtils.getRoleService().addGuiQi(role,rewardUnitSet.getCount(),true);
                break;
            case Const.REWARD_TYPE_jinyuanbao:
                SpringUtils.getRoleService().addGold(role,rewardUnitSet.getCount(), Reason.BUY_ITEM);
                break;
            case Const.REWARD_TYPE_yinyuanbao:
                SpringUtils.getRoleService().addSiver(role,rewardUnitSet.getCount(),Reason.BUY_ITEM);
                break;
            case Const.REWARD_TYPE_banggong:
                SpringUtils.getRoleService().addPartyContrib(role, rewardUnitSet.getCount());
                break;

        }
    }

    public void addRewardItem(Role role, RewardUnitSet rewardUnitSet, int count) {
        if(rewardUnitSet.getStoreType() ==0 || rewardUnitSet.getStoreType() ==2){
            int num = rewardUnitSet.getCount()==0?1:rewardUnitSet.getCount();
            rewardUnitSet.setCount(count * num);
            addRewardItem(role, rewardUnitSet);
        }else {
            for (int i=0; i<count; i++) {
                addRewardItem(role, rewardUnitSet);
            }
        }
    }
}
